﻿using System.Xml.Linq;
using System.Xml.Serialization;

namespace Nardax.ServiceModel.Discovery
{
    static class XmlSerializerExtensions
    {
        public static XElement SerializeAsXElement(this XmlSerializer serializer, object o)
        {
            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "");

            var document = new XDocument();

            using (var w = document.CreateWriter())
            {
                serializer.Serialize(w, o, namespaces);
            }

            var root = document.Root;

            if (root != null)
            {
                root.Remove();
            }

            return root;
        }
    }
}
